package com.huazhi.wzsnw.controller;

import com.huazhi.wzsnw.mapper.DdlinedMapper;
import com.huazhi.wzsnw.mapper.LinedMapper;
import com.huazhi.wzsnw.model.*;
import com.huazhi.wzsnw.service.*;
import com.huazhi.wzsnw.util.redis.RedisCache;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/smoke")
public class SmokeController {
    @Autowired
    private SmokeService smokeService;
    @Autowired
    private AlertService alertService;
    @Autowired
    private LinedMapper linedMapper;
    @Autowired
    private DdlinedMapper ddlinedMapper;
    @Autowired
    private RedisCache redisCache;

    @RequestMapping(value = "/getLinedAll", method ={RequestMethod.POST})
    public ModelAndView getLinedAll(Model model, HttpServletRequest request,
                                 HttpSession httpSession) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        JSONObject jsonObject = new JSONObject();
        String name = request.getParameter("cun");
        List<Lined> lineds = new ArrayList<>();
        List<Ddlined> ddlineds = new ArrayList<>();
        redisCache.setCacheObject("zch","测试redis",10, TimeUnit.SECONDS);
        if("no".equals(name)||null == name){
         //地图初始化 查询所有设备
           lineds = linedMapper.getAll();
           ddlineds = ddlinedMapper.getAll();
           List<Lined> linedList = ddLinedToLined(ddlineds);
           lineds.addAll(linedList);
        }else if("1".equals(name)||"0".equals(name)||"2".equals(name)||"3".equals(name)){
            lineds = linedMapper.selectBuState(name);
            ddlineds = ddlinedMapper.selectBuState(name);
            List<Lined> linedList = ddLinedToLined(ddlineds);
            lineds.addAll(linedList);
        }else if("4".equals(name)){
          lineds = linedMapper.selectOnlySxt();
        }
        else {
            lineds =linedMapper.selectByname(name);
            ddlineds = ddlinedMapper.selectByname(name);
            List<Lined> linedList = ddLinedToLined(ddlineds);
            lineds.addAll(linedList);
        }
        if(lineds.size()!=0){
            Timestamp defaultTime = new Timestamp(System.currentTimeMillis());
            Date date = new Date();
            List<Lined> lineds1 = new ArrayList<>();
            for(Lined one :lineds){
                if("1".equals(one.getState())){
                    Alert alert = new Alert();
                    alert.setNumber(one.getNumber());
                    List<Alert> list = alertService.getAlertListByClassify(alert,null,null);
                   if(list.size()>0){
                       one.setReason(list.get(0).getReason());
                   }
                }
                if("2".equals(one.getState())){
                   one.setReason("原因:dtu断开连接,数据为采集器最后一次发送的数据");
                }
                if("3".equals(one.getState())){
                   one.setReason("原因:dtu内部异常,获取不到设备数据,数据为采集器最后一次发送的数据");
                }
                if(null == one.getNumber()&& one.getVideoUrl()!= null){
                    one.setState("4");
                    one.setReason("原因:采集器还未安装");
                }
                if(one.getChanshuibengchangetime()==null){
                    one.setChanshuibengchangetimeString("");
                    one.setChanshuibengchangetime(defaultTime);
                }else{
                    one.setChanshuibengchangetimeString(sdf.format(one.getChanshuibengchangetime()));
                }
                if(one.getFanxibengchangetime()==null){
                    one.setFanxibengchangetimeString("");
                    one.setFanxibengchangetime(defaultTime);
                }else{
                    one.setFanxibengchangetimeString(sdf.format(one.getFanxibengchangetime()));
                }
                if(one.getJiaobanjichangetime()==null){
                    one.setJiaobanjichangetimeString("");
                    one.setJiaobanjichangetime(defaultTime);
                }else{
                    one.setJiaobanjichangetimeString(sdf.format(one.getJiaobanjichangetime()));
                }
                if(one.getGufengji1changetime()==null){
                    one.setGufengji1changetimeString("");
                    one.setGufengji1changetime(defaultTime);
                }else{
                    one.setGufengji1changetimeString(sdf.format(one.getGufengji1changetime()));
                }

                if(one.getGufengji2changetime()==null){
                    one.setGufengji2changetimeString("");
                    one.setGufengji2changetime(defaultTime);
                }else{
                    one.setGufengji2changetimeString(sdf.format(one.getGufengji2changetime()));
                }
                if(one.getJinshuibengchangetime()==null){
                    one.setJinshuibengchangetimeString("");
                    one.setJinshuibengchangetime(defaultTime);
                }else{
                    one.setJinshuibengchangetimeString(sdf.format(one.getJinshuibengchangetime()));
                }
                if(one.getHuiliubengchangetime()==null){
                    one.setHuiliubengchangetimeString("");
                    one.setHuiliubengchangetime(defaultTime);
                }else{
                    one.setHuiliubengchangetimeString(sdf.format(one.getHuiliubengchangetime()));
                }
                if(one.getCreateTime()==null){
                    one.setCreateTime(defaultTime);
                }
                if(one.getYwrq() == null){
                    one.setYwrq(defaultTime);
                }
                if(one.getJgrq() == null){
                    one.setJgrq(defaultTime);
                }
                if(one.getJsrq() == null){
                    one.setJsrq(defaultTime);
                }
                if(one.getNumber()!=null){
                   lineds1.add(one);
                }
            }
            jsonObject.put("data",lineds1);
            jsonObject.put("msg","");
            jsonObject.put("count",lineds1.size());
            jsonObject.put("code",0);
        }else {
            jsonObject.put("data",lineds);
            jsonObject.put("msg","");
            jsonObject.put("count",lineds.size());
            jsonObject.put("code",0);
        }
        return  json(jsonObject);
    }

    @RequestMapping(value = "/getLined", method ={RequestMethod.POST})
    public ModelAndView getLined(Model model, HttpServletRequest request,
                                     HttpSession httpSession) {
        try {
            User user = (User) httpSession.getAttribute("userInfo");
            JSONObject jsonObject = new JSONObject();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String search = request.getParameter("key");
            String sxxzt = request.getParameter("sxtzt");
            List<String> keys = new ArrayList<String>();
            if ("".equals(search) || search == null) {
                keys.add("");
            } else if (!"".equals(search) && search.indexOf(" ") != -1) {
                keys = Arrays.asList(search.split(" "));
            } else if (!"".equals(search) && search.indexOf(" ") == -1) {
                keys.add(search);
            }
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            Integer pages = Integer.valueOf(page);
            Integer limits = Integer.valueOf(limit);
            Integer pageIndex = (pages - 1) * limits;
            Integer pageSize = Integer.valueOf(limit);
            List<String> list = new ArrayList<String>();
            Boolean firstSearch = true;
            for (String oneParam : keys) {
                if (firstSearch) {
                    list = linedMapper.selectListTidByKeyWithUrl(oneParam,sxxzt);
                    firstSearch = false;
                } else {
                    List<String> middleList = linedMapper.selectListTidByKeyWithUrl(oneParam,sxxzt);
                    list.retainAll(middleList);
                }
            }
            List<Lined> lineds = new ArrayList<Lined>();
            Integer total = 0;
            if(list.size()==0){
                jsonObject.put("data",false);
                jsonObject.put("count",0);
                jsonObject.put("msg","");
            }else {
                lineds = linedMapper.findPagingByListTid(list, pageIndex, pageSize);
                total = list.size();
                Timestamp defaultTime = new Timestamp(System.currentTimeMillis());
                if(lineds.size()>0){
                    for(Lined one :lineds){
                        if(one.getChanshuibengchangetime()==null){
                            one.setChanshuibengchangetimeString("");
                            one.setChanshuibengchangetime(defaultTime);
                        }else{
                            one.setChanshuibengchangetimeString(sdf.format(one.getChanshuibengchangetime()));
                        }
                        if(one.getFanxibengchangetime()==null){
                            one.setFanxibengchangetimeString("");
                            one.setFanxibengchangetime(defaultTime);
                        }else{
                            one.setFanxibengchangetimeString(sdf.format(one.getFanxibengchangetime()));
                        }
                        if(one.getJiaobanjichangetime()==null){
                            one.setJiaobanjichangetimeString("");
                            one.setJiaobanjichangetime(defaultTime);
                        }else{
                            one.setJiaobanjichangetimeString(sdf.format(one.getJiaobanjichangetime()));
                        }
                        if(one.getGufengji1changetime()==null){
                            one.setGufengji1changetimeString("");
                            one.setGufengji1changetime(defaultTime);
                        }else{
                            one.setGufengji1changetimeString(sdf.format(one.getGufengji1changetime()));
                        }

                        if(one.getGufengji2changetime()==null){
                            one.setGufengji2changetimeString("");
                            one.setGufengji2changetime(defaultTime);
                        }else{
                            one.setGufengji2changetimeString(sdf.format(one.getGufengji2changetime()));
                        }
                        if(one.getJinshuibengchangetime()==null){
                            one.setJinshuibengchangetimeString("");
                            one.setJinshuibengchangetime(defaultTime);
                        }else{
                            one.setJinshuibengchangetimeString(sdf.format(one.getJinshuibengchangetime()));
                        }
                        if(one.getHuiliubengchangetime()==null){
                            one.setHuiliubengchangetimeString("");
                            one.setHuiliubengchangetime(defaultTime);
                        }else{
                            one.setHuiliubengchangetimeString(sdf.format(one.getHuiliubengchangetime()));
                        }
                        if(one.getCreateTime()==null){
                            one.setCreateTime(defaultTime);
                        }
                        if(one.getYwrq() == null){
                            one.setYwrq(defaultTime);
                        }
                        if(one.getJgrq() == null){
                            one.setJgrq(defaultTime);
                        }
                        if(one.getJsrq() == null){
                            one.setJsrq(defaultTime);
                        }
                    }
                }
                jsonObject.put("data",lineds);
                jsonObject.put("count",total);
                jsonObject.put("msg","");
            }
            jsonObject.put("code",0);
            return  json(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            return  json(null);
        }
    }
    @RequestMapping(value = "/getDdLinedByNumber", method ={RequestMethod.GET})
    public  ModelAndView getDdLinedByNumber(HttpServletRequest request){
       String number = request.getParameter("number");
       Ddlined ddlined = ddlinedMapper.selectByDtuNum(number);
       return json(ddlined);
    }


    @RequestMapping(value = "/getOneLined", method ={RequestMethod.GET})
    public ModelAndView getOneLined(Model model, HttpServletRequest request,
                                 HttpSession httpSession) {
        try {
            User user = (User) httpSession.getAttribute("userInfo");
            String tid = request.getParameter("linedTid");
            JSONObject jsonObject = new JSONObject();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Lined lineds = linedMapper.selectByPrimaryKey(tid);
            Integer total = 0;

            if(lineds==null){
                jsonObject.put("data",false);
                jsonObject.put("count",0);
                jsonObject.put("msg","");
            }else {
                total = 1;

                Timestamp defaultTime = new Timestamp(System.currentTimeMillis());
                        if(lineds.getChanshuibengchangetime()==null){
                            lineds.setChanshuibengchangetimeString("");
                            lineds.setChanshuibengchangetime(defaultTime);
                        }else{
                            lineds.setChanshuibengchangetimeString(sdf.format(lineds.getChanshuibengchangetime()));
                        }
                        if(lineds.getFanxibengchangetime()==null){
                            lineds.setFanxibengchangetimeString("");
                            lineds.setFanxibengchangetime(defaultTime);
                        }else{
                            lineds.setFanxibengchangetimeString(sdf.format(lineds.getFanxibengchangetime()));
                        }
                        if(lineds.getJiaobanjichangetime()==null){
                            lineds.setJiaobanjichangetimeString("");
                            lineds.setJiaobanjichangetime(defaultTime);
                        }else{
                            lineds.setJiaobanjichangetimeString(sdf.format(lineds.getJiaobanjichangetime()));
                        }
                        if(lineds.getGufengji1changetime()==null){
                            lineds.setGufengji1changetimeString("");
                            lineds.setGufengji1changetime(defaultTime);
                        }else{
                            lineds.setGufengji1changetimeString(sdf.format(lineds.getGufengji1changetime()));
                        }

                        if(lineds.getGufengji2changetime()==null){
                            lineds.setGufengji2changetimeString("");
                            lineds.setGufengji2changetime(defaultTime);
                        }else{
                            lineds.setGufengji2changetimeString(sdf.format(lineds.getGufengji2changetime()));
                        }
                        if(lineds.getJinshuibengchangetime()==null){
                            lineds.setJinshuibengchangetimeString("");
                            lineds.setJinshuibengchangetime(defaultTime);
                        }else{
                            lineds.setJinshuibengchangetimeString(sdf.format(lineds.getJinshuibengchangetime()));
                        }
                        if(lineds.getHuiliubengchangetime()==null){
                            lineds.setHuiliubengchangetimeString("");
                            lineds.setHuiliubengchangetime(defaultTime);
                        }else{
                            lineds.setHuiliubengchangetimeString(sdf.format(lineds.getHuiliubengchangetime()));
                        }

                if(lineds.getCreateTime()==null){
                    lineds.setCreateTime(defaultTime);
                }
                if(lineds.getYwrq() == null){
                    lineds.setYwrq(defaultTime);
                }
                if(lineds.getJgrq() == null){
                    lineds.setJgrq(defaultTime);
                }
                if(lineds.getJsrq() == null){
                    lineds.setJsrq(defaultTime);
                }
                jsonObject.put("data",lineds);
                jsonObject.put("count",total);
                jsonObject.put("msg","");
            }
            jsonObject.put("code",0);
            return  json(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            return  json(null);
        }
    }

    @RequestMapping(value = "/getOneLinedByCun", method ={RequestMethod.GET})
    public ModelAndView getOneLinedByCun(Model model, HttpServletRequest request,
                                    HttpSession httpSession) {
        try {
            User user = (User) httpSession.getAttribute("userInfo");
            String cun = request.getParameter("cun");
            JSONObject jsonObject = new JSONObject();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Lined lineds = linedMapper.seletLineByCun(cun);
            Integer total = 0;

            if(lineds==null){
                jsonObject.put("data",false);
                jsonObject.put("count",0);
                jsonObject.put("msg","");
            }else {
                total = 1;

                Timestamp defaultTime = new Timestamp(System.currentTimeMillis());

                if(lineds.getChanshuibengchangetime()==null){
                    lineds.setChanshuibengchangetimeString("");
                    lineds.setChanshuibengchangetime(defaultTime);
                }else{
                    lineds.setChanshuibengchangetimeString(sdf.format(lineds.getChanshuibengchangetime()));
                }
                if(lineds.getFanxibengchangetime()==null){
                    lineds.setFanxibengchangetimeString("");
                    lineds.setFanxibengchangetime(defaultTime);
                }else{
                    lineds.setFanxibengchangetimeString(sdf.format(lineds.getFanxibengchangetime()));
                }
                if(lineds.getJiaobanjichangetime()==null){
                    lineds.setJiaobanjichangetimeString("");
                    lineds.setJiaobanjichangetime(defaultTime);
                }else{
                    lineds.setJiaobanjichangetimeString(sdf.format(lineds.getJiaobanjichangetime()));
                }
                if(lineds.getGufengji1changetime()==null){
                    lineds.setGufengji1changetimeString("");
                    lineds.setGufengji1changetime(defaultTime);
                }else{
                    lineds.setGufengji1changetimeString(sdf.format(lineds.getGufengji1changetime()));
                }

                if(lineds.getGufengji2changetime()==null){
                    lineds.setGufengji2changetimeString("");
                    lineds.setGufengji2changetime(defaultTime);
                }else{
                    lineds.setGufengji2changetimeString(sdf.format(lineds.getGufengji2changetime()));
                }
                if(lineds.getJinshuibengchangetime()==null){
                    lineds.setJinshuibengchangetimeString("");
                    lineds.setJinshuibengchangetime(defaultTime);
                }else{
                    lineds.setJinshuibengchangetimeString(sdf.format(lineds.getJinshuibengchangetime()));
                }
                if(lineds.getHuiliubengchangetime()==null){
                    lineds.setHuiliubengchangetimeString("");
                    lineds.setHuiliubengchangetime(defaultTime);
                }else{
                    lineds.setHuiliubengchangetimeString(sdf.format(lineds.getHuiliubengchangetime()));
                }

                if(lineds.getCreateTime()==null){
                    lineds.setCreateTime(defaultTime);
                }
                if(lineds.getYwrq() == null){
                    lineds.setYwrq(defaultTime);
                }
                if(lineds.getJgrq() == null){
                    lineds.setJgrq(defaultTime);
                }
                if(lineds.getJsrq() == null){
                    lineds.setJsrq(defaultTime);
                }
                jsonObject.put("data",lineds);
                jsonObject.put("count",total);
                jsonObject.put("msg","");
            }
            jsonObject.put("code",0);
            return  json(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            return  json(null);
        }
    }

    @RequestMapping(value = "/getOneDdLinedByNumber",method = RequestMethod.GET)
    public  ModelAndView getOneDdLinedByNumber(HttpServletRequest request){
       String number = request.getParameter("number");
       Ddlined ddlined = ddlinedMapper.selectByDtuNum(number);
        com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        if (ddlined.getJsrq() != null) {
            ddlined.setJsrqString(format.format(ddlined.getJsrq()));
        }
        if (ddlined.getJgrq() != null) {
            ddlined.setJgrqString(format.format(ddlined.getJgrq()));
        }
        if (ddlined.getYwrq() != null) {
            ddlined.setYwrqString(format.format(ddlined.getYwrq()));
        }
        jsonObject.put("data",ddlined);
        jsonObject.put("count",1);
        jsonObject.put("msg","");
        jsonObject.put("code",0);
        return  json(jsonObject);
    }


    @RequestMapping(value = "/getLinedSearch", method ={RequestMethod.GET})
    public ModelAndView getLinedSearch(Model model, HttpServletRequest request,
                                       HttpSession httpSession) {
//        try {
            User user = (User) httpSession.getAttribute("userInfo");
            JSONObject jsonObject = new JSONObject();
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            String search = request.getParameter("key");
            List<String> keys = new ArrayList<String>();
            if ("".equals(search) || search == null) {
                keys.add("");
            } else if (!"".equals(search) && search.indexOf(" ") != -1) {
                keys = Arrays.asList(search.split(" "));
            } else if (!"".equals(search) && search.indexOf(" ") == -1) {
                keys.add(search);
            }

            List<String> list = new ArrayList<String>();
            Boolean firstSearch = true;
            for (String oneParam : keys) {
                if (firstSearch) {
                    list = linedMapper.selectListTidByKey(oneParam);
                    firstSearch = false;
                } else {
                    List<String> middleList = linedMapper.selectListTidByKey(oneParam);
                    list.retainAll(middleList);
                }
            }
            List<Lined> lineds = new ArrayList<Lined>();
            Integer total = 0;
            if(list.size()==0){
                jsonObject.put("data",false);
                jsonObject.put("count",0);
                jsonObject.put("msg","");
            }else {
                lineds = linedMapper.findPagingByListTid(list, null, null);
                total = list.size();
                Timestamp defaultTime = new Timestamp(System.currentTimeMillis());
                if(lineds.size()>0){
                    for(Lined one :lineds){
                        if(one.getChanshuibengchangetime()==null){
                            one.setChanshuibengchangetimeString("");
                            one.setChanshuibengchangetime(defaultTime);
                        }else{
                            one.setChanshuibengchangetimeString(sdf.format(one.getChanshuibengchangetime()));
                        }
                        if(one.getFanxibengchangetime()==null){
                            one.setFanxibengchangetimeString("");
                            one.setFanxibengchangetime(defaultTime);
                        }else{
                            one.setFanxibengchangetimeString(sdf.format(one.getFanxibengchangetime()));
                        }
                        if(one.getJiaobanjichangetime()==null){
                            one.setJiaobanjichangetimeString("");
                            one.setJiaobanjichangetime(defaultTime);
                        }else{
                            one.setJiaobanjichangetimeString(sdf.format(one.getJiaobanjichangetime()));
                        }
                        if(one.getGufengji1changetime()==null){
                            one.setGufengji1changetimeString("");
                            one.setGufengji1changetime(defaultTime);
                        }else{
                            one.setGufengji1changetimeString(sdf.format(one.getGufengji1changetime()));
                        }

                        if(one.getGufengji2changetime()==null){
                            one.setGufengji2changetimeString("");
                            one.setGufengji2changetime(defaultTime);
                        }else{
                            one.setGufengji2changetimeString(sdf.format(one.getGufengji2changetime()));
                        }
                        if(one.getJinshuibengchangetime()==null){
                            one.setJinshuibengchangetimeString("");
                            one.setJinshuibengchangetime(defaultTime);
                        }else{
                            one.setJinshuibengchangetimeString(sdf.format(one.getJinshuibengchangetime()));
                        }
                        if(one.getHuiliubengchangetime()==null){
                            one.setHuiliubengchangetimeString("");
                            one.setHuiliubengchangetime(defaultTime);
                        }else{
                            one.setHuiliubengchangetimeString(sdf.format(one.getHuiliubengchangetime()));
                        }
                        if(one.getCreateTime()==null){
                            one.setCreateTime(defaultTime);
                        }
                        if(one.getYwrq() == null){
                            one.setYwrq(defaultTime);
                        }
                        if(one.getJgrq() == null){
                            one.setJgrq(defaultTime);
                        }
                        if(one.getJsrq() == null){
                            one.setJsrq(defaultTime);
                        }
                    }
                }
                jsonObject.put("data",lineds);
                jsonObject.put("count",total);
                jsonObject.put("msg","");
            }
            jsonObject.put("code",0);
            return  json(jsonObject);

    }

    @RequestMapping(value = "/getSmokeByTid"  , method={RequestMethod.POST})
    public ModelAndView getSmokeByTid(Model model, HttpServletRequest request,
                                                    HttpSession httpSession) {
        try{
            String tid=request.getParameter("tid");
            Smoke smoke=smokeService.getSmokeByTid(tid);
            return json(smoke);
        }catch (Exception e){
            e.printStackTrace();
            return json(null);
        }
    }

    @RequestMapping(value = "/updateSmoke"  , method={RequestMethod.POST})
    public ModelAndView updateSmoke(Model models, HttpServletRequest request,
                                    HttpSession httpSession) {
        try{
            User user=(User)httpSession.getAttribute("userInfo");
            String tid=request.getParameter("tid");
            String model=request.getParameter("model");
            String install=request.getParameter("install");
            String adress=request.getParameter("adress");
            String longitude=request.getParameter("longitude");
            String latitude=request.getParameter("latitude");
            Smoke smoke=smokeService.getSmokeByTid(tid);
            smoke.setModel(model);
            smoke.setInstall(install);
            smoke.setAdress(adress);
            if("NaN".equals(longitude)){
                smoke.setLongitude("");
            }else {
                smoke.setLongitude(longitude);
            }
            if("NaN".equals(latitude)){
                smoke.setLatitude("");
            }else {
                smoke.setLatitude(latitude);
            }
            String departmentTid = request.getParameter("departmentTid[0]");
            String responsibletid = request.getParameter("departmentTid[1]");
            if("".equals(responsibletid)){
                smoke.setResponsibletid(user.getTid());
            }else {
                smoke.setResponsibletid(responsibletid);
            }
            smoke.setDepartmenttid(departmentTid);
            smokeService.updateByPrimaryKeySelective(smoke);
            return json("ok");
        }catch (Exception e){
            e.printStackTrace();
            return json("error");
        }
    }
    @RequestMapping(value = "/getSmokeAlert",  method = { RequestMethod.POST, RequestMethod.GET })
    public void   getSmokeAlert(HttpServletRequest request,HttpServletResponse response) {
        try {

            String smokes = request.getParameter("smoke");
            InputStreamReader  reader=new InputStreamReader(request.getInputStream());
            BufferedReader buffer=new BufferedReader(reader);
            String data=buffer.readLine();
            System.out.println(data);

            //头部信息
            String header=request.getHeader("headerdata");
            System.out.println(header);
            //向客户端发送数据
            response.setContentType("text/javascript");

            PrintWriter out=response.getWriter();
            out.write("");


        }catch (Exception e){
            e.printStackTrace();

        }
    }

    @RequestMapping(value = "/getSmoke"  , method={RequestMethod.POST})
    public ModelAndView getSmoke(Model model, HttpServletRequest request,
                                            HttpSession httpSession) {
        try{
            User user=(User)httpSession.getAttribute("userInfo");
            List<Smoke> smokeList=smokeService.getSomke(user.getTid());
            return json(smokeList);
        }catch (Exception e){
            e.printStackTrace();
            return json(null);
        }
    }


    @RequestMapping(value = "/lAYindextopCard", method={RequestMethod.GET})
    public ModelAndView  lAYindextopCard(Model model, HttpServletRequest request,
                                      HttpSession httpSession) {
        try{
            JSONObject jsonObject = new JSONObject();
            User user=(User)httpSession.getAttribute("userInfo");
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            List<Smoke> smokeList=smokeService.getSomkelAYindextopCard(user.getTid());
            Date date=new Date();
            for(Smoke smoke:smokeList){
                smoke.setLastTimeString(sdf.format(smoke.getLastTime()));
                Date lastDate=smoke.getLastTime();
                Long l=date.getTime()-lastDate.getTime();
                Long min=(l/(1000*60));
                smoke.setMin(min);
            }
            jsonObject.put("data",smokeList);
            jsonObject.put("count",smokeList.size());
            jsonObject.put("msg","");
            jsonObject.put("code",0);
            return json(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            return json(null);
        }
    }
    //各设备总数
    @RequestMapping(value = "/deviceCount", method={RequestMethod.POST})
    public ModelAndView  deviceCount(Model model, HttpServletRequest request,
                                         HttpSession httpSession) {
        try{
            JSONObject jsonObject = new JSONObject();
            User user=(User)httpSession.getAttribute("userInfo");
            Integer smokeCount=smokeService.getDeviceCount(user.getTid(),"烟感");
            jsonObject.put("smokeCount",smokeCount);
            return json(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            return json(null);
        }
    }

    //类型报警或故障
    @RequestMapping(value = "/deviceStateCount", method={RequestMethod.POST})
    public ModelAndView  deviceStateCount(Model model, HttpServletRequest request,
                                    HttpSession httpSession) {
        try{
            JSONObject jsonObject = new JSONObject();
            User user=(User)httpSession.getAttribute("userInfo");
            Integer smokeStateCount=smokeService.getDeviceStateCount(user.getTid(),"烟感","报警");
            jsonObject.put("smokeStateCount",smokeStateCount);
            return json(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            return json(null);
        }
    }

    //物联网终端状态一览表
    @RequestMapping(value = "/getStatistics", method={RequestMethod.POST})
    public ModelAndView  getStatistics(Model model, HttpServletRequest request,
                                          HttpSession httpSession) {
        try{
            JSONObject jsonObject = new JSONObject();
            User user=(User)httpSession.getAttribute("userInfo");
            List<String> state=new ArrayList<String>();
            state.add("正常");
            state.add("报警");
            state.add("故障");
            state.add("低电压");
            List<String> type=new ArrayList<String>();
            type.add("烟感");
            type.add("水感");
            type.add("井盖");
            type.add("灯感");
            type.add("水表");
            type.add("电表");
            jsonObject.put("state",state);
            jsonObject.put("type",type);

            List<JSONObject> DSF=new ArrayList<JSONObject>();
            for (String state1:state){
                JSONObject jsonObject1=new JSONObject();
                jsonObject1.put("name",state1);
                jsonObject1.put("type","line");
                JSONObject jsonObject2=new JSONObject();
                JSONObject jsonObject3=new JSONObject();
                jsonObject3.put("show",true);
                jsonObject3.put("position","top");
                jsonObject2.put("normal",jsonObject3);
                jsonObject1.put("label",jsonObject2);
                List<Integer> count=new ArrayList<Integer>();
                for(String type1:type) {
                    Integer smokeStateCount=smokeService.getDeviceStateCount(user.getTid(),type1,state1);
                    count.add(smokeStateCount);
                }
                jsonObject1.put("data",count);
                DSF.add(jsonObject1);
            }
            jsonObject.put("data",DSF);
            return json(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            return json(null);
        }
    }

    private  List<Lined> ddLinedToLined(List<Ddlined> ddlineds){
        List<Lined> result = new ArrayList<>();

        for (Ddlined ddlined :ddlineds){
            Lined lined = new Lined();
            lined.setTid(ddlined.getTid());
            lined.setNumber(ddlined.getNumber());
            lined.setState(ddlined.getState());
            lined.setZhen(ddlined.getZhen());
            lined.setCun(ddlined.getCun());
            lined.setVideotid(ddlined.getVideotid());
            lined.setVideoUrl(ddlined.getVideourl());
            lined.setJingdu(ddlined.getJingdu());
            lined.setWeidu(ddlined.getWeidu());
            lined.setName(ddlined.getName());
            lined.setDunwei(ddlined.getDunwei());
            lined.setCunweihui(ddlined.getCunweihui());
            lined.setSsbm(ddlined.getSsbm());
            result.add(lined);
        }
        return  result;
    }

    private ModelAndView json(Object result) {
        return new ModelAndView("jsonView").addObject("result", result);
    }
}
